﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConcurrantLoadingLib
{
    public class TradeLoader
    {
        public IEnumerable<Trade> GetTrades(string path)
        {
            foreach (var parsedDataRow in ParseCsv(path))
            {
                var orderId = parsedDataRow[0].Trim();
                var ticker = parsedDataRow[1].Trim();
                var quantity = Convert.ToInt32(parsedDataRow[2].Trim());
                var price = Convert.ToDouble(parsedDataRow[3].Trim());
                var trade = new Trade(orderId, ticker, quantity, price);
                yield return trade;
            }
        }

        private IEnumerable<string[]> ParseCsv(string path)
        {
            //var rnd = new Random();
            using (var readFile = new StreamReader(path))
            {
                string line;
                while ((line = readFile.ReadLine()) != null)
                {
                    var row = line.Split(',');
                    yield return row;

                    // sleep to simulate a slow loader
                    Thread.Sleep(250);
                    //Thread.Sleep(Convert.ToInt32(500 * rnd.NextDouble()));
                }
            }
        }
    }
}
